//
//  GanLuHomeVCViewController.m
//  GanLuApp
//
//  Created by 暗流澎涌 on 2024/4/1.
//

#import "GanLuHomeVCViewController.h"
#import "GanLuShudongDateVC.h"
#import <AVKit/AVKit.h>
#import <Photos/Photos.h>
#import "GanLuWebLoadPage.h"
#import "GanLuNewMeVC.h"


typedef NS_ENUM(NSInteger, AuthorizationStatus) {
    /**不确定*/
    AuthorizationStatusNotDetermined,//不确定，此时系统尚未向用户申请权限
    /**用户禁止该权限*/
    AuthorizationStatusDenied,
    /**用户已经授权*/
    AuthorizationStatusAuthorized
};

@interface GanLuHomeVCViewController ()
@property (weak, nonatomic) IBOutlet UIButton *setbtn;

@end

@implementation GanLuHomeVCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (IBAction)click_muyu:(id)sender {
    GanLuWebLoadPage *page = [[GanLuWebLoadPage alloc] init];
    page.url = @"http://ganlu.ktfza.top/muyu/ganlu/index.html";
    [self.navigationController pushViewController:page animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (IBAction)click_Shudong:(id)sender {
    
    AuthorizationStatus sta = [self canUseCamera];
    
    if(sta == AuthorizationStatusNotDetermined){
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                // 已授权访问相册
                NSLog(@"已授权访问相册");
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    GanLuShudongDateVC * vc = [[GanLuShudongDateVC alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                });
            } else {
                // 未授权访问相册
//                NSLog(@"未授权访问相册");
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [self showalert];
//                });
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    GanLuShudongDateVC * vc = [[GanLuShudongDateVC alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                });
            }
        }];
        return;
    }
    GanLuShudongDateVC * vc = [[GanLuShudongDateVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)showalert {
    UIAlertController *ale = [UIAlertController alertControllerWithTitle:@"提示" message:@"请前往设置打开相册使用权限" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ac1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *ac2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }];
    [ale addAction:ac1];
    [ale addAction:ac2];
    [self presentModalViewController:ale animated:YES];
}

- (IBAction)click_SetmeVC:(id)sender {
    GanLuNewMeVC *vc = [[GanLuNewMeVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (AuthorizationStatus)canUseCamera {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
        return AuthorizationStatusDenied;
    }
    if (status == AVAuthorizationStatusAuthorized) {
        return AuthorizationStatusAuthorized;
    }
    return AuthorizationStatusNotDetermined;
}

@end

 
